package com.ccb.lms.action;

import com.ccb.lms.facade.FacadeService;
import com.ccb.lms.model.Reader;
import com.ccb.lms.util.Validity;

public class ReaderAction extends AbstractAction{

	private FacadeService facadeService;
	private Reader reader;
	
	private String readerPassword;
	private String readerPasswordNew;
	private String readerPasswordNewConfirm;
	
	
	private String readerName;
	private String readerSex;
	private String readerMail;
	private String readerTelephone;
	private String readerDepartment;
	private String readerRemark;

	

	
	
	@Override
	public String execute() throws Exception {
		
		return super.execute();
	}

	
	public String GetReaderInfo() throws Exception{
		reader=(Reader)session.get("reader");
		if (reader != null) {
			reader = facadeService.getReaderService().getReaderByID(reader.getReaderID());
			session.put("reader", reader);
			return "SUCCESS";
		}
		return "INPUT";
		
	}
	
	public String ReaderChangePasswd() throws Exception{
		reader=(Reader)session.get("reader");
		if (reader != null) {
			reader = facadeService.getReaderService().getReaderByID(reader.getReaderID());
			if(reader.getReaderPassword().equals(readerPassword)){
				reader.setReaderPassword(readerPasswordNew);
				if(facadeService.getReaderService().updateReader(reader)){	
					session.put("reader", reader);
					return "SUCCESS";
				}
				else{
					actionErrorMessage="密码更新错误";
					return "ERROR";
				}
			}
			else{
				actionErrorMessage="原密码输入错误";
				return "STAY";
			}
		}
		return "INPUT";
		
		
	}
	
	//TODO
	public String ReaderChangeUserInfo() throws Exception{
		reader=(Reader)session.get("reader");
		if (reader != null) {
			reader = facadeService.getReaderService().getReaderByID(reader.getReaderID());
			reader.setReaderName(readerName.trim());
			
			if(Validity.isEmailFormat(readerMail.trim())){
				reader.setReaderMail(readerMail.trim());
			}else{
				actionErrorMessage="邮箱输入格式错误";
				return "STAY";
			}
			
			if(Validity.isPhoneNumber(readerTelephone)){
				reader.setReaderTelephone(readerTelephone.trim());
			}else{
				actionErrorMessage="电话输入格式错误";
				return "STAY";
			}
			
			reader.setReaderDepartment(readerDepartment.trim());
			if(readerSex.equalsIgnoreCase("male")){
				reader.setReaderSex("男");
			}else{
				reader.setReaderSex("女");
			}
			reader.setReaderRemark(readerRemark);
			
			
			if(facadeService.getReaderService().updateReader(reader)){	
					session.put("reader", reader);
					return "SUCCESS";
			}
			else{
					actionErrorMessage="用户更新失败";
					return "ERROR";
			}
		}
			
		return "INPUT";
		
		
	}

	public FacadeService getFacadeService() {
		return facadeService;
	}

	public void setFacadeService(FacadeService facadeService) {
		this.facadeService = facadeService;
	}

	public Reader getReader() {
		return reader;
	}

	public void setReader(Reader reader) {
		this.reader = reader;
	}


	public String getReaderPassword() {
		return readerPassword;
	}


	public void setReaderPassword(String readerPassword) {
		this.readerPassword = readerPassword;
	}


	public String getReaderPasswordNew() {
		return readerPasswordNew;
	}


	public void setReaderPasswordNew(String readerPasswordNew) {
		this.readerPasswordNew = readerPasswordNew;
	}


	public String getReaderPasswordNewConfirm() {
		return readerPasswordNewConfirm;
	}


	public void setReaderPasswordNewConfirm(String readerPasswordNewConfirm) {
		this.readerPasswordNewConfirm = readerPasswordNewConfirm;
	}


	public String getReaderName() {
		return readerName;
	}


	public void setReaderName(String readerName) {
		this.readerName = readerName;
	}


	public String getReaderSex() {
		return readerSex;
	}


	public void setReaderSex(String readerSex) {
		this.readerSex = readerSex;
	}


	public String getReaderMail() {
		return readerMail;
	}


	public void setReaderMail(String readerMail) {
		this.readerMail = readerMail;
	}


	public String getReaderDepartment() {
		return readerDepartment;
	}


	public void setReaderDepartment(String readerDepartment) {
		this.readerDepartment = readerDepartment;
	}


	public String getReaderRemark() {
		return readerRemark;
	}


	public void setReaderRemark(String readerRemark) {
		this.readerRemark = readerRemark;
	}


	public String getReaderTelephone() {
		return readerTelephone;
	}


	public void setReaderTelephone(String readerTelephone) {
		this.readerTelephone = readerTelephone;
	}


	
	

}
